// import { createStore } from 'redux'
//1.导入创建全局状态的方法 createStore
import { legacy_createStore as createStore } from "redux";

// 2.定义一个 reducer 纯函数
//该函数中有两个参数
//参数一就是 state ,该state就是全局状态中的数据
//参数二 actions 中有两个属性, 第一个type 标记我们要做的事情 payload 就是参数
const reducer = (
  state = {
    count: 100,
  },
  action
) => {
  const { type, payload } = action;

  console.log(type, payload);

  switch (type) {
    case "add":
      return { ...state, count: state.count + 1 };

    case "reduce":
      return { ...state, count: state.count - 1 };

    case "addNum":
      return { ...state, count: state.count + payload };

    case "reduceNum":
      return { ...state, count: state.count - payload };
    default:
      return state;
  }
};

//3.生成状态管理器
const store = createStore(reducer);

// 4.将 store 暴露出去
export default store;
